package com.wanxs.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxs.mall.common.dto.SkuReductionDTO;
import com.wanxs.mall.common.service.impl.CrudServiceImpl;
import com.wanxs.mall.coupon.dao.SkuFullReductionDao;
import com.wanxs.mall.coupon.dto.SkuFullReductionDTO;
import com.wanxs.mall.coupon.entity.MemberPriceEntity;
import com.wanxs.mall.coupon.entity.SkuFullReductionEntity;
import com.wanxs.mall.coupon.entity.SkuLadderEntity;
import com.wanxs.mall.coupon.service.MemberPriceService;
import com.wanxs.mall.coupon.service.SkuFullReductionService;
import com.wanxs.mall.coupon.service.SkuLadderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品满减信息
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-04
 */
@Service
public class SkuFullReductionServiceImpl extends CrudServiceImpl<SkuFullReductionDao, SkuFullReductionEntity, SkuFullReductionDTO> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService ladderService;

    @Autowired
    private MemberPriceService memberPriceService;


    @Override
    public QueryWrapper<SkuFullReductionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SkuFullReductionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionDTO dto) {
        // 5.3 保存满减信息，折扣，会员价
        // mall_sms: sms_sku_ladder sms_full_reduction sms_member_price
        // 1.折扣
        SkuLadderEntity ladderEntity = new SkuLadderEntity();
        ladderEntity.setSkuId(dto.getSkuId());
        ladderEntity.setFullCount(dto.getFullCount());
        ladderEntity.setDiscount(dto.getDiscount());
        ladderEntity.setAddOther(dto.getCountStatus());
        if(ladderEntity.getFullCount() > 0){
            ladderService.insert(ladderEntity);
        }
        // 2.满减
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(dto,fullReductionEntity);
        if(fullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) == 1){
            this.insert(fullReductionEntity);
        }
        // 3.会员价
        List<MemberPriceEntity> memberPriceEntities = dto.getMemberPrice().stream().map(item -> {
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(dto.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(1); // 是否可叠加
            return priceEntity;
        }).collect(Collectors.toList());
        memberPriceService.insertBatch(memberPriceEntities);
    }
}